package com.sc.nft.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.dto.AddAppVersionDTO;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.QueryAppVersionRequest;
import com.sc.nft.entity.dto.UpdateAppVersionDTO;
import com.sc.nft.entity.AppVersion;
import com.sc.nft.entity.vo.AppVersionVO;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.service.AppVersionService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * app版本控制器
 *
 * @author Tangzehui
 * @description app版本控制
 * @since 2023-10-25
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/app")
@Api(tags = "APP版本")
public class AppVersionController {

    private final AppVersionService appVersionService;

    @ApiOperation(value = "新增版本信息", notes = "新增版本信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/saveVersion", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result saveAppVersion(@Valid @RequestBody AddAppVersionDTO addAppVersionDTO) throws GlobalException {
        if (!appVersionService.saveAppVersion(addAppVersionDTO)) {
            throw new GlobalException(ErrorCode.OPERATE_FAIL);
        }
        return Result.success();
    }

    @ApiOperation(value = "删除版本信息", notes = "删除版本信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/deleteVersion", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteAppVersion(@Valid @RequestBody IdRequest idRequest) throws GlobalException {
        if (!appVersionService.deleteAppVersion(idRequest.getId())) {
            throw new GlobalException(ErrorCode.OPERATE_FAIL);
        }
        return Result.success();
    }

    @ApiOperation(value = "更新版本信息", notes = "更新版本信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateVersion", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateAppVersion(@Valid @RequestBody UpdateAppVersionDTO updateAppVersionDTO) throws GlobalException {
        if (ObjectUtil.isNotNull(appVersionService.findById(updateAppVersionDTO.getId()))) {
            if (!appVersionService.updateAppVersion(updateAppVersionDTO)) {
                throw new GlobalException(ErrorCode.FAIL_TO_EDIT);
            }
            return Result.success();
        }
        return Result.fail(ErrorCode.FAIL_TO_EDIT);
    }

    @ApiOperation(value = "查询版本信息", notes = "查询版本信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/query", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AppVersionVO> queryAppVersion(@Valid @RequestBody QueryAppVersionRequest queryAppVersionRequest) {
        Page<AppVersion> result = appVersionService.query(queryAppVersionRequest.getType(), queryAppVersionRequest.getPageNo(), queryAppVersionRequest.getPageSize());
        result.convert(version -> BeanUtil.copyProperties(version, AppVersionVO.class));
        return PageResult.success(result);
    }


}


